package com.soujiayi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.soujiayi.adapter.TuanCityListAdapter;
import com.soujiayi.model.TuanCity;
import com.soujiayi.net.HttpRequester;
import com.soujiayi.net.NetworkUtils;
import com.soujiayi.store.DatabaseManager;
import com.soujiayi.util.CityParser;
import com.soujiayi.util.Constants;
import com.soujiayi.view.TuanCityGridView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import android.widget.GridView;
import android.widget.ListView;

public class TuanSelectCityActivity extends Activity {
	private TuanCityGridView gv_city_hot;
	private ListView lv_city_area;
	private View headerView;
	private LinearLayout ll_city_loading;
	private Handler handler;
	private DatabaseManager databaseManager = new DatabaseManager(this);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.city_select_main);
		initViews();
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				ll_city_loading.setVisibility(View.GONE);
				if (msg.what == 1) {
					ArrayList<TuanCity> temCityList = (ArrayList<TuanCity>) msg.obj;
					ArrayList<TuanCity> tuanCityList = new ArrayList<TuanCity>();
					if (temCityList == null || temCityList.size() == 0) {
						TuanCity city = new TuanCity();
						city.setCityid("3");
						city.setName(TuanSelectCityActivity.this
								.getString(R.string.tuan_city_default));
						city.setOrder("999");
						city.setFirstchar("S");
						city.setPinyin("shanghai");
						tuanCityList.add(city);
					} else
						tuanCityList = temCityList;
					databaseManager.insertCities(tuanCityList);
					initAdapter();
				} else if (msg.what == 2) {
					initAdapter();
				}
			}

		};
		initActionListener();
		initTuanCities();
	}

	public void initViews() {
		headerView = getLayoutInflater().inflate(R.layout.city_select_header,
				null);
		ll_city_loading = (LinearLayout) this
				.findViewById(R.id.ll_city_loading);

		gv_city_hot = (TuanCityGridView) headerView
				.findViewById(R.id.gv_city_hot);
		lv_city_area = (ListView) findViewById(R.id.lv_city_area);
		lv_city_area.addHeaderView(headerView);

	}

	public void initActionListener() {
		gv_city_hot.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				TuanSelectCityActivity.this.setResult(
						Activity.RESULT_OK,
						TuanSelectCityActivity.this.getIntent().putExtra(
								Constants.TUAN_CITY_CITYNAME,
								((TextView) view
										.findViewById(R.id.tv_city_name))
										.getText().toString()));
				TuanSelectCityActivity.this.finish();
				overridePendingTransition(R.anim.push_down_in,
						R.anim.push_down_out);
			}

		});
	}

	public void initTuanCities() {
		ll_city_loading.setVisibility(View.VISIBLE);
		if (!NetworkUtils.isNetworkConnected(this)) {
			Toast.makeText(this,
					this.getString(R.string.network_unavailable_message),
					Toast.LENGTH_SHORT);
			Message msg = new Message();
			msg.what = 1;
			msg.obj = null;
			handler.sendMessage(msg);
			return;
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message msg = new Message();
				if (databaseManager.getCount(Constants.CITIES_TABLE_NAME) <= 1) {
					msg.what = 1;
					Map<String, String> parameters = new HashMap<String, String>();
					parameters.put("sid",
							((MyApplication) TuanSelectCityActivity.this
									.getApplication()).getSessionId());
					ArrayList<TuanCity> temCityList = new HttpRequester(
							TuanSelectCityActivity.this)
							.getTuanCities(parameters);
					msg.obj = temCityList;
				} else
					msg.what = 2;
				handler.sendMessage(msg);
			}
		}).start();
	}

	public void initAdapter() {
		ArrayList<LinkedHashMap<String, Object>> areaCities = new ArrayList<LinkedHashMap<String, Object>>();
		ArrayList<String> hotCities = new ArrayList<String>();
		areaCities = databaseManager.getAreaCities();
		hotCities = databaseManager.getHotCities();
		gv_city_hot.setAdapter(new ArrayAdapter<String>(this,
				R.layout.city_select_grid_item, hotCities));
		lv_city_area.setAdapter(new TuanCityListAdapter(this, areaCities));
	}

	public void backToHome(View view) {
		Intent intent = new Intent(TuanSelectCityActivity.this,
				IndexActivity.class);
		startActivity(intent);
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

	public void back(View view) {
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

}
